package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import admin.data.EmailData;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import models.EmailModel;
import models.GeoModel;
import admin.data.GeoData;

public class ExportEmailExcelServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		GeoData geoData = new GeoData();
		ArrayList<GeoModel> cities = geoData.getCities();
		
		String city = "";
		
		request.setAttribute("cities", cities);
		request.setAttribute("city", city);
		try {
			request.getRequestDispatcher("/admin/pages/export_email.jsp").forward(request, response);
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		HttpSession session = request.getSession();
		
		GeoData geoData = new GeoData();
		ArrayList<GeoModel> cities = geoData.getCities();
		
		String city = "";
		
		city = (String) request.getParameter("city");
		if (city == null || city.isEmpty()) {
			session.setAttribute("error", "Bạn chưa chọn thành phố.");
			response.sendRedirect("/admin/email/export");
			return ;
		}
		
		EmailData emailData = new EmailData();
		ArrayList<EmailModel> emails = emailData.getByCity(city);
		if (emails.size() > 0) {
			try {
				writeExcel(response, emails);
				return ;
			} catch (Exception e) {
				// TODO: handle exception
			}
		} else {
			session.setAttribute("infor", "Không có email nào trong danh sách email ");
		}
		
		request.setAttribute("cities", cities);
		request.setAttribute("city", city);
		try {
			request.getRequestDispatcher("/admin/pages/export_email.jsp").forward(request, response);
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
	}
	
	private void writeExcel(HttpServletResponse response, ArrayList<EmailModel> emails) 
		throws IOException, WriteException {
			// Setting uo the response properties
	
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment; filename=export-email.xls");
	
			//Creating the workbook
			WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
	
			// Creating sheet in the workbook
			WritableSheet sheet = workbook.createSheet("Schedule", 0);
	
			// Adding cells to the sheet
			sheet.addCell(new Label(0, 0, "Email"));
			for (int i = 1; i <= emails.size(); i++) {
				sheet.addCell(new Label(0, i, emails.get(i-1).getEmail()));
			}
			
	
			//Writing the data to the workbook
			workbook.write();
	
			//Closing the workbook
			workbook.close();
	}
}
